using System.Collections.Generic;
using System.Windows.Forms;
using RollManager.Forms.ActionPanels;
using RollManager.Forms.DetailsPanels;
using RollManager.Library;
using System.Drawing;
using RollManager.Forms.MenuDialogs;
using RollManager.Library.ActionSteps;

namespace RollManager.Forms.MainPanels
{
    public partial class AdminActionsPanel : UserControl, MainPanel
    {
        private UserControl m_detailPanel;

        private ItemBase SelectedTag
        {
            get
            {
                if( mainTreeView.SelectedNode == null )
                {
                    return null;
                }

                return mainTreeView.SelectedNode.Tag as ItemBase;
            }
        }

        public AdminActionsPanel()
        {
            InitializeComponent();
        }

        public override void Refresh()
        {
            base.Refresh();
            refreshTree();
        }

        private void mainTreeView_AfterSelect( object sender, TreeViewEventArgs e )
        {
            splitContainer.Panel2.Controls.Clear();
            m_detailPanel = null;

            if( e.Node.Text == "Servers" )
            {
                m_detailPanel = new ServersDetailsPanel();
            }
            else if( e.Node.Tag is Action )
            {
                m_detailPanel = new ActionDetailsPanel( (Action) e.Node.Tag );
            }
            else if( e.Node.Tag is ServerGroup )
            {
                m_detailPanel = new ServerGroupsDetailsPanel( (ServerGroup) e.Node.Tag, this );
            }
            else if( e.Node.Tag is SubAction )
            {
                m_detailPanel = new SubActionDetailsPanel( (SubAction) e.Node.Tag );
            }

            if( m_detailPanel != null)
            {
                m_detailPanel.Dock = DockStyle.Fill;
                splitContainer.Panel2.Controls.Add( m_detailPanel );
            }
        }

        private void mainTreeView_BeforeSelect( object sender, TreeViewCancelEventArgs e )
        {
            Save();

            if( SelectedTag != null )
            {
                string name = SelectedTag.Name;
                mainTreeView.SelectedNode.Name = name;
                mainTreeView.SelectedNode.Text = name;
            }
        }

        private void mainTreeView_MouseClick( object sender, MouseEventArgs e )
        {
            if( e.Button != MouseButtons.Right )
            {
                return;
            }

            ContextMenuStrip menu = null;
            TreeNode node = mainTreeView.GetNodeAt( e.Location );
            if( node.Text == "Servers" )
            {
                menu = serversMenu;
            }
            else if( node.Text == "Server Groups")
            {
                menu = serverGroupsMenu;
            }
            else if( node.Tag == null )
            {
                return;
            }
            else if( node.Tag is DevLevel )
            {
                menu = devLevelMenu;
            }
            else if( node.Tag is Action)
            {
                menu = actionMenu;

                Action action = (Action) node.Tag;
                if( action.Saveable )
                {
                    menu.Items.Insert( 0, addSubActionToolStripMenuItem );
                    menu.Items.Insert( 2, deleteToolStripMenuItem );
                }
                else
                {
                    menu.Items.Remove( addSubActionToolStripMenuItem );
                    menu.Items.Remove( deleteToolStripMenuItem );
                }
            }
            else if(node.Tag is ServerGroup)
            {
                List<SubAction> subActions = Database.GetSubActionsByServerGroup( ( (ServerGroup) node.Tag ).Id );
                if( subActions.Count == 0 )
                {
                    menu = serverGroupMenu;
                }
            }
            else if(node.Tag is SubAction)
            {
                menu = subActionMenu;

                SubAction subAction = (SubAction) node.Tag;
                if( subAction.Saveable )
                {
                    menu.Items.Insert( 1, moveToolStripMenuItem );
                    menu.Items.Insert( 2, deleteToolStripMenuItem1 );
                }
                else
                {
                    menu.Items.Remove( moveToolStripMenuItem );
                    menu.Items.Remove( deleteToolStripMenuItem1 );
                }
            }

            if( menu != null )
            {
                mainTreeView.SelectedNode = node;
                Point screenPoint = mainTreeView.PointToScreen( e.Location );
                Point formPoint = PointToClient( screenPoint );
                menu.Show( this, formPoint );
            }
        }

        private void addActionToolStripMenuItem_Click( object sender, System.EventArgs e )
        {
            AddActionDialog dialog = new AddActionDialog( this, SelectedTag.Id );
            dialog.ShowDialog();
        }

        private void addSubActionToolStripMenuItem_Click( object sender, System.EventArgs e )
        {
            AddSubActionDialog dialog = new AddSubActionDialog( this, SelectedTag.Id );
            dialog.ShowDialog();
        }

        private void addServerGroupToolStripMenuItem_Click( object sender, System.EventArgs e )
        {
            AddServerGroupDialog dialog = new AddServerGroupDialog( this );
            dialog.ShowDialog();
        }

        private void addServerToolStripMenuItem_Click( object sender, System.EventArgs e )
        {
            AddServerDialog dialog = new AddServerDialog( this );
            dialog.ShowDialog();
        }

        private void copyToolStripMenuItem_Click( object sender, System.EventArgs e )
        {
            CopySubActionDialog dialog = new CopySubActionDialog( this, (SubAction) SelectedTag, false );
            dialog.ShowDialog();
        }

        private void copyToolStripMenuItem1_Click( object sender, System.EventArgs e )
        {
            CopyActionDialog dialog = new CopyActionDialog( this, (Action) SelectedTag );
            dialog.ShowDialog();
        }

        private void deleteToolStripMenuItem_Click( object sender, System.EventArgs e )
        {
            if( Common.ConfirmAction( "Are you sure you wish to delete this?", "Confirm Deletion" ) )
            {
                Database.DeleteAction( SelectedTag.Id );
                deleteCurrentNode();
            }
        }

        private void deleteToolStripMenuItem1_Click( object sender, System.EventArgs e )
        {
            if( Common.ConfirmAction( "Are you sure you wish to delete this?", "Confirm Deletion" ) )
            {
                Database.DeleteSubAction( SelectedTag.Id );
                deleteCurrentNode();
            }
        }

        #region MainPanel Members

        public void Save()
        {
            if( m_detailPanel != null )
            {
                ( (DetailsPanel) m_detailPanel ).Save();
            }
        }

        #endregion

        private void simulateToolStripMenuItem_Click( object sender, System.EventArgs e )
        {
            Save();

            SubAction subAction = (SubAction) SelectedTag;
            Action action = (Action) mainTreeView.SelectedNode.Parent.Tag;
            DevLevel devLevel = (DevLevel) mainTreeView.SelectedNode.Parent.Parent.Tag;

            ActionDialog.StartAction( devLevel, action, subAction, true );
        }

        private void toolStripMenuItem1_Click( object sender, System.EventArgs e )
        {
            if( Common.ConfirmAction( "Are you sure you wish to delete this?", "Confirm Deletion" ) )
            {
                Database.DeleteServerGroup( SelectedTag.Id );
                deleteCurrentNode();
            }
        }

        private void mainTreeView_DoubleClick( object sender, MouseEventArgs e )
        {
            // Make sure a valid sub action was selected.
            TreeNode node = mainTreeView.GetNodeAt( e.Location );
            if( node.Tag == null || !( node.Tag is SubAction ) )
            {
                return;
            }

            mainTreeView.SelectedNode = node;
            performActionToolStripMenuItem_Click( sender, e );
        }

        private void performActionToolStripMenuItem_Click( object sender, System.EventArgs e )
        {
            Save();

            SubAction subAction = (SubAction) SelectedTag;
            Action action = (Action) mainTreeView.SelectedNode.Parent.Tag;
            DevLevel devLevel = (DevLevel) mainTreeView.SelectedNode.Parent.Parent.Tag;

            ActionDialog.StartAction( devLevel, action, subAction, false );
        }

        private void historyToolStripMenuItem_Click( object sender, System.EventArgs e )
        {
            Save();

            SubAction subAction = (SubAction) SelectedTag;
            Action action = (Action) mainTreeView.SelectedNode.Parent.Tag;
            DevLevel devLevel = (DevLevel) mainTreeView.SelectedNode.Parent.Parent.Tag;

            Common.ShowHistory( devLevel, action, subAction );
        }

        private void moveToolStripMenuItem_Click( object sender, System.EventArgs e )
        {
            CopySubActionDialog dialog = new CopySubActionDialog( this, (SubAction) SelectedTag, true );
            dialog.ShowDialog();
        }
    }
}